package com.ztgf.pay.api.client;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.pay.models.bo.PayUserMoneySaveBO;
import com.ztgf.pay.models.bo.PayUserRechargeSaveBO;
import com.ztgf.pay.models.bo.PayUserWithdrawBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.ztgf.pay.models.service.IPayUserRechargeService;
import com.ztgf.pay.models.bo.PayUserRechargeBO;
import com.ztgf.pay.models.dto.PayUserRechargeDTO;

import java.util.List;

/**
 * 用户充值表控制器
 *
 * @author mxli
 * @Date 2020-07-30 12:15:07
 */

@RestController
@RequestMapping("/api/payUserRecharge")
@Slf4j
@Api(value = "PayUserRechargeApiController",tags = "用户充值表Api接口")
public class PayUserRechargeApiController {

    @DubboReference
    private IPayUserRechargeService payUserRechargeService;

    @PostMapping("/saveRechargeByOnlineBank")
    @ApiOperation(value = "网银充值",notes = "网银充值")
    public Result<PayUserRechargeDTO> saveRechargeByOnlineBank(PayUserRechargeSaveBO saveBO){
        return payUserRechargeService.saveRechargeByOnlineBank(saveBO);
    }

    @PostMapping("/saveWithdraw")
    @ApiOperation(value = "提现到银行卡",notes = "提现到银行卡")
    public Result saveWithdraw(PayUserWithdrawBO saveBO){
        return payUserRechargeService.saveWithdraw(saveBO);
    }

    @PostMapping("/saveFreezeMoney")
    @ApiOperation(value = "批量冻结金额",notes = "批量冻结金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userRecharges",value = "[{}],userId:用户id,money:金额,accountType:账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "String")
    })
    public Result<List<PayUserRechargeDTO>> saveFreezeMoney(String userRecharges){
        if(StrUtils.isEmpty(userRecharges)){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        List<PayUserMoneySaveBO> list = JSONObject.parseArray(userRecharges, PayUserMoneySaveBO.class);
        return payUserRechargeService.saveFreezeMoney(list);
    }

    @PostMapping("/saveUnfreezeMoney")
    @ApiOperation(value = "批量解冻金额",notes = "批量解冻金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userRecharges",value = "[{}],userId:用户id,money:金额,accountType:账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "String")
    })
    public Result<List<PayUserRechargeDTO>> saveUnfreezeMoney(String userRecharges){
        if(StrUtils.isEmpty(userRecharges)){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        List<PayUserMoneySaveBO> list = JSONObject.parseArray(userRecharges, PayUserMoneySaveBO.class);
        return payUserRechargeService.saveUnfreezeMoney(list);
    }

    @PostMapping("/saveMoney")
    @ApiOperation(value = "解冻/冻结余额",notes = "解冻/冻结余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userRecharges",value = "[{}],userId:用户id,money:金额,accountType:账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "String")
            ,@ApiImplicitParam(name = "type",value = "0冻结、1解冻",required = true,paramType = "query", dataType = "Integer")
    })
    public Result<List<PayUserRechargeDTO>> saveMoney(String userRecharges, int type){
        if(StrUtils.isEmpty(userRecharges)){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        List<PayUserMoneySaveBO> list = JSONObject.parseArray(userRecharges, PayUserMoneySaveBO.class);

        return payUserRechargeService.saveMoney(list, type);
    }

    @ApiOperation(value = "用户充值表列表",notes = "用户充值表列表")
    @PostMapping("/queryPageList")
    public Result<Page<PayUserRechargeDTO>> queryPageList(PayUserRechargeBO bo){
        Wrapper<PayUserRechargeDTO> wrapper = new EntityWrapper<>();

        wrapper.eq("user_id", bo.getUserId());
        if(bo.getType() != null){
            wrapper.eq("type", bo.getType());
        }

        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<PayUserRechargeDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = payUserRechargeService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/saveMoneyLog")
    @ApiOperation(value = "主动查询更新出入金状态",notes = "主动查询更新出入金状态")
    public void saveMoneyLog(){
        Wrapper wrapper = new EntityWrapper();
        wrapper.and("(pay_type = 1 or type = 1)");
        Object[] status = {0, 1};
        wrapper.in("status", status);
        List<PayUserRechargeDTO> list = payUserRechargeService.selectList(wrapper);
        if(list != null && list.size() > 0){
            for (PayUserRechargeDTO dto : list) {
                payUserRechargeService.saveMoneyLog(dto);
            }
        }

    }

}
